import axios from 'axios'
import store from '@/store'

const service = axios.create({
  baseURL: 'http://am7.cloudee-soft.com/api/',
  timeout: 5000
})

// request 拦截器
service.interceptors.request.use(
  config => {
    if (store.state.auth.token) {
      config.headers.Authorization = `${store.state.auth.token}`
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.success) {
      return Promise.resolve(res.data)
    } else {
      return Promise.reject(res.msg)
    }
  },
  error => {
    console.log(error)
    return Promise.reject(error.response.data)
  }
)

export default service
